<div class="layui-form" lay-filter="layuiadmin-form-convert-download">
    <script type="text/html" template>
        <input type="hidden" name="id" value="{{ d.params.id || '0' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <script type="text/html" template>
        <input type="hidden" name="taskName" value="{{ d.params.taskName || '' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <script type="text/html" template>
        <input type="hidden" name="billType" value="{{ d.params.billType || '' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <script type="text/html" template>
        <input type="hidden" name="invoiceNumber" value="{{ d.params.invoiceNumber || '' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <script type="text/html" template>
        <input type="hidden" name="eticketNumber" value="{{ d.params.eticketNumber || '' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <script type="text/html" template>
        <input type="hidden" name="issueDateStart" value="{{ d.params.issueDateStart || '' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <script type="text/html" template>
        <input type="hidden" name="issueDateEnd" value="{{ d.params.issueDateEnd || '' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <script type="text/html" template>
        <input type="hidden" name="uploadTimeStart" value="{{ d.params.uploadTimeStart || '' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <script type="text/html" template>
        <input type="hidden" name="uploadTimeEnd" value="{{ d.params.uploadTimeEnd || '' }}" lay-verify="" placeholder="" autocomplete="off" class="layui-input">
    </script>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width:100px;">下载格式</label>
        <div class="layui-input-inline">
            <script type="text/html" template>
                <select name="fileFormat" lay-filter="fileFormat" {{= d.params.id!=undefined? 'disabled': '' }}>
                    <option value="">全部</option>
                    <option value="1" {{= d.params.fileFormat=="1"? 'selected': '' }}>OFD</option>
                    <option value="2" {{= d.params.fileFormat=="2"? 'selected': '' }}>PDF</option>
                    <option value="3" {{= d.params.fileFormat=="3"? 'selected': ''}}>XML</option>
                </select>
            </script>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width:100px;">自定义文件名称</label>
        <div class="layui-input-block">
            <input type="checkbox" name="arr[issueDate]" lay-skin="tag" title="开票日期" lay-filter="convert-downloadform-checkbox-filter" checked />
            <input type="checkbox" name="arr[invoiceNumber]" lay-skin="tag" title="发票号码" lay-filter="convert-downloadform-checkbox-filter" checked>
            <input type="checkbox" name="arr[eticketNumber]" lay-skin="tag" title="电子客票号" lay-filter="convert-downloadform-checkbox-filter" checked>
            <input type="checkbox" name="arr[buyerName]" lay-skin="tag" title="购方名称" lay-filter="convert-downloadform-checkbox-filter" checked>
            <input type="checkbox" name="arr[total]" lay-skin="tag" title="总金额" lay-filter="convert-downloadform-checkbox-filter" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width:100px;"></label>
        <div class="layui-input-block">
            <label class="layui-font-gray" id="LAY-convert-download-fileNameDemo">示例文件名称：</label>
        </div>
    </div>
    <div class="layui-form-item" style="text-align:center;">
        <button class="layui-btn" lay-on="cancelConvertDownloadForm">取消</button>
        <input type="button" lay-submit lay-filter="LAY-convert-download-front-submit" value="开始下载" class="layui-btn">
    </div>
</div>
<script>
    layui.use(['admin', 'form', 'upload'], function () {
        var $ = layui.$
            , form = layui.form
            , upload = layui.upload;

        function getFileNameDemo() {
            let extName = ".ofd";
            let fileName = "示例文件名称：";

            //表单取值
            var formData = form.val('layuiadmin-form-convert-download');
            console.log(formData);

            //获取文件扩展名
            if (formData.fileFormat == '2') {
                extName = ".pdf";
            }
            else if (formData.fileFormat == '3') {
                extName = ".xml";
            }

            //获取自定义文件名称
            let isCheckIssueDate = formData['arr[issueDate]'] != null && formData['arr[issueDate]'] == 'on';
            let isCheckInvoiceNumber = formData['arr[invoiceNumber]'] != null && formData['arr[invoiceNumber]'] == 'on';
            let isCheckEticketNumber = formData['arr[eticketNumber]'] != null && formData['arr[eticketNumber]'] == 'on';
            let isCheckBuyerName = formData['arr[buyerName]'] != null && formData['arr[buyerName]'] == 'on';
            let isCheckTotal = formData['arr[total]'] != null && formData['arr[total]'] == 'on';

            //发票号码和电子客票号至少选择一个
            if (!isCheckInvoiceNumber && !isCheckEticketNumber) {
                layer.msg('发票号码和电子客票号至少选择一个!');
                return "";
            }

            if (isCheckIssueDate) {
                fileName += "2024-12-01_";
            }
            if (isCheckInvoiceNumber) {
                fileName += "12345678901234567890_";
            }
            if (isCheckEticketNumber) {
                fileName += "010-1234567890_";
            }
            if (isCheckBuyerName) {
                fileName += "xxx有限公司_";
            }
            if (isCheckTotal) {
                fileName += "100.00_";
            }
            fileName = fileName.replace(/_+$/, '');
            return fileName + extName;
        }

        //设置示例文件名称
        var demoFileName = getFileNameDemo();
        $('#LAY-convert-download-fileNameDemo').text(demoFileName);

        //选择下载格式事件
        form.on('select(fileFormat)', function (data) {
            var demoFileName = getFileNameDemo();
            $('#LAY-convert-download-fileNameDemo').text(demoFileName);
        });

        //自定义文件名称checkbox事件
        form.on('checkbox(convert-downloadform-checkbox-filter)', function (data) {
            var demoFileName = getFileNameDemo();
            $('#LAY-convert-download-fileNameDemo').text(demoFileName);
        });

        //上传头像
        upload.render({
            elem: '#layuiadmin-upload-useradmin'
            , url: './res/json/upload/demo.js'
            , accept: 'images'
            , method: 'get'
            , acceptMime: 'image/*'
            , done: function (res) {
                $(this.item).prev("div").children("input").val(res.data.src)
            }
        });
    })
</script>